package myapp.view 
{
	import com.bumpslide.ui.Image;
	import com.bumpslide.ui.Lightbox;

	import flash.events.Event;

	/**
	 * Image Popup Lightbox
	 * 
	 * @author David Knape
	 */
	public class ImagePopup extends Lightbox 
	{

		// the image we are displaying
		public var image:Image;

		// the image url, of course
		private var _url:String;

		// validation constant
		private static const VALID_IMAGE_URL:String = "validImageUrl";

		
		/**
		 * Unload image when lightbox is closed
		 */
		override public function close(e:Event = null):void 
		{
			super.close(e);
			image.unload();
		}

		
		/**
		 * on redraw, check for image change, and update display if necessary
		 */		
		override protected function draw():void
		{
			if(hasChanged(VALID_IMAGE_URL)) {
				
				validate(VALID_IMAGE_URL);
				
				// if we have a URL to load, load it
				// and open the lightbox with this image
				if(url != null) {
					image = new Image( url, Image.SCALE_RESIZE, 500, 500, eventDelegate(invalidate, VALID_SIZE) );
					image.fadeOnLoad = true;
					open(image);
				} else {
					close();
				}				
			}
			super.draw();
		}

		
		/**
		 * The URL of the image to load
		 */		
		public function get url():String {
			return _url;
		}

		
		public function set url(imageUrl:String):void {
			if(_url == imageUrl) return;
			_url = imageUrl;			
			// trigger redraw, and note that image url has changed
			invalidate(VALID_IMAGE_URL);			
		}
	}
}
